local errorSoundCVar = "Sound_EnableErrorSpeech";
local oldErrorSound = nil;
local frame = CreateFrame("Frame");

local funcAllowNS

function SarfFix2:PreventErrors()
	UIErrorsFrame:UnregisterEvent("UI_ERROR_MESSAGE");
	SarfFix2:ShowErrorsLater(1, SarfFix2.AllowErrors);
end

function SarfFix2:AllowErrors()
	UIErrorsFrame:RegisterEvent("UI_ERROR_MESSAGE");
	if ( shouldAllowFunc == SarfFix2.AllowErrors ) then
		frame:Hide();
	end
end

function SarfFix2:PreventErrorsSound()
	SarfFix2:PreventErrors();
	oldErrorSound = GetCVar(errorSoundCVar);
	SetCVar(errorSoundCVar,"0")
	SarfFix2:ShowErrorsLater(1, SarfFix2.AllowErrorsSound);
end

function SarfFix2:AllowErrorsSound()
	SarfFix2:AllowErrors();
	SetCVar(errorSoundCVar,oldErrorSound)
	oldErrorSound = nil;
	if ( shouldAllowFunc == SarfFix2.AllowErrorsSound ) then
		frame:Hide();
	end
end

function SarfFix2:ShowErrorsLater(time, func)
	shouldAllowTime = GetTime() + time;
	if ( type(func) == "string" ) then
		shouldAllowFunc = SarfFix2[func];
	else
		shouldAllowFunc = func;
	end
	frame:Show();
end

function SarfFix2:UIErrorsFrameOnUpdate()
	local t = GetTime();
	if ( not shouldAllowTime ) or ( t > shouldAllowTime ) then
		if ( type(shouldAllowFunc) == "function" ) then
			shouldAllowFunc(SarfFix2);
		end
		frame:Hide();
	end
end

function SarfFix2:InitUIErrorsFrame()
	frame:Hide();
	frame:SetScript("OnUpdate", function() SarfFix2:UIErrorsFrameOnUpdate() end);
end


SarfFix2:RegisterSlashCmd({"/sfpe"}, function() SarfFix2:PreventErrors(); end, "PreventErrors");
SarfFix2:RegisterSlashCmd({"/sfae"}, function() SarfFix2:AllowErrors(); end, "AllowErrors");
SarfFix2:RegisterSlashCmd({"/sfpes"}, function() SarfFix2:PreventErrorsSound(); end, "PreventErrorsSound");
SarfFix2:RegisterSlashCmd({"/sfaes"}, function() SarfFix2:AllowErrorsSound(); end, "AllowErrorsSound");